﻿using System;
using System.Collections.Generic;
using FairyGUI;

public class FSuitEditPopup : GComponent
{
    GList _list;
    List<Suit> _dataList;
    OneDelegate _callBack;
    string _weaponId;

    public override void ConstructFromXML(FairyGUI.Utils.XML cxml)
    {
        base.ConstructFromXML(cxml);

        _list = this.GetChild("list").asList;
        _list.itemRenderer = RenderItem;

        this.GetChild("addToSuit").onClick.Add(OnAddToSuitClick);
    }

    public void InitData(List<Suit>dataList,string weaponId,OneDelegate cb)
    {
        _weaponId = weaponId;
        _callBack = cb;
        _dataList = dataList;
        _list.RemoveChildrenToPool();
        _list.numItems = _dataList.Count;
    }

    private void RenderItem(int index,GObject item)
    {
        item.text = _dataList[index].GetSuitName();
        item.asCom.GetChild("removeBtn").onClick.Add(OnRemoveSuitBtn);
        item.asCom.GetChild("title").onClick.Add(OnEditSuitClick);
    }

    private void OnRemoveSuitBtn(EventContext context)
    {
        var index = _list.GetChildIndex(((GObject)context.sender).parent);
        SuitMO.RevemoSuitWeapon(_dataList[index].id,_weaponId);
        _dataList.RemoveAt(index);
        _list.RemoveChildToPoolAt(index);
    }

    private void OnEditSuitClick(EventContext context)
    {
        var panel = (FEditSuitMediator)FUIMain.ShowPanel(GMConfig.PanelType.EditSuitPanel);
        var index = _list.GetChildIndex(((GObject)context.sender).parent);
        panel.InitData(_dataList[index],SuitMO.GetSuitIndex(_dataList[index]));
    }
    private void OnAddToSuitClick(EventContext context)
    {
        FUIMain.ShowPanel(GMConfig.PanelType.SelectSuitList,(res) => {
            FUIMain.ClosePanel(GMConfig.PanelType.SelectSuitList);
            _callBack(res);
        });
    }
}

